package monopoly;

import java.util.Random;

public class CardDeck {
	
	private Card[] cards;
	private int index;
	
	
	public CardDeck()
	{
		cards = new Card[16];
		index = 0;
	}
	
	public CardDeck(String type)
	{
		if (type.equals("Community Chest"))
			initCommunityChest();
		else if (type.equals("Chance"))
			initChance();
		else
			cards = new Card[16];
		index = 0;
	}
	
	public void resetIndex()
	{
		index = 0;
	}
	
	public Card drawCard()
	{
		if (index > cards.length) 
		{
			resetIndex();
			shuffle();
		}
		Card card = cards[index];
		index++;
		return card;
	}
	
	private void initCommunityChest()
	{	
		cards = new Card[16];
		cards[0] = new Card("Advance token to Go.  \nCollect $200.", 0, true);
		cards[1] = new Card("Bank error in your favor.  \nCollect $200.", 200.0, "Bank");
		cards[2] = new Card("Doctor's fee.  \nPay $50.", -50.0, "Bank");
		cards[3] = new Card("From sale of stock \nyou get $45.", 45.0, "Bank");
		cards[4] = new Card("Get out of Jail free.  \nThis card my be kept \nuntil needed, or sold.", true);
		cards[5] = new Card("Go to jail.  \nGo directly to Jail.  \nDo not pass Go.  \nDo not collect $200.", 40, false);
		cards[6] = new Card("Grand opera opening.  \nCollect $50 from opponent \nfor opening night seats.", 50.0, "Player");
		cards[7] = new Card("Income tax refund.  \nCollect $20.", 20.0, "Bank");
		cards[8] = new Card("Life insurance matures.  \nCollect $100.", 100.0, "Bank");
		cards[9] = new Card("Pay hospital $100", -100.0, "Bank");
		cards[10] = new Card("Pay school tax of $150.", -150.0, "Bank");
		cards[11] = new Card("Receive for services $25.", 25.0, "Bank");
		cards[12] = new Card("Xmas fund matures.  \nCollect $100.", 100.0, "Bank");
		cards[13] = new Card("You are assessed for \nstreet repairs.  \n\n$40 per house, \n$115 per hotel.", false);
		cards[14] = new Card("You have won second \nprize in a beauty contest.  \nCollect $10.", 10.0, "Bank");
		cards[15] = new Card("You inherit $100.", 100.0, "Bank");
		
		for (int index=0 ; index<cards.length ; index++)
		{
			cards[index].setType("Community Chest");
		}
		
		shuffle();
	}
	
	private void initChance()
	{
		cards = new Card[16];
		cards[0] = new Card("Advance to Go.  \nCollect $200.", 0, true);
		cards[1] = new Card("Advance to Illinois.", 24, true);
		cards[2] = new Card("Advance token to the \nnearest Utility and\n pay owner 10 times \namount thrown on dice.", "Utility", true, 0, 10);
		cards[3] = new Card("Advance token to the \nnearest Railroad and \npay owner twice the \nrent to which he/she \nis otherwise entitled.", "Railroad", true, 2, 0);
		cards[4] = new Card("Advance token to the \nnearest Railroad and \npay owner twice the \nrent to which he/she \nis otherwise entitled.", "Railroad", true, 2, 0);
		cards[5] = new Card("Advance to St. Charles Place.", 11, true);
		cards[6] = new Card("Bank pays you \ndividend of $50.", 50.0, "Bank");
		cards[7] = new Card("Get out of jail free.  \nThis card may be kept \nuntil needed, or sold.", true);
		cards[8] = new Card("Go back three spaces.", -3);
		cards[9] = new Card("Go to Jail.  \nGo directly to jail.  \nDo not pass go.  \nDo not collect $200.", 40, false);
		cards[10] = new Card("Make general repairs \non your property.  \nFor each house, pay $25.  \nFor each hotel, pay $100.", false);
		cards[11] = new Card("Pay poor tax of $15.", -15, "Bank");
		cards[12] = new Card("Take a ride on the Reading.  \nIf you pass Go, collect $200.", 5, true);
		cards[13] = new Card("Take a walk on the Boardwalk.  \nAdvance token to Boardwalk.", 39, true);
		cards[14] = new Card("You have been elected \nchairman of the board.  \nPay opponent $50.", -50.0, "Player");
		cards[15] = new Card("Your building and \nloan matures.  \nCollect $150.", 150.0, "Bank");	
		
		for (int index=0 ; index < cards.length ; index++)
		{
			cards[index].setType("Chance");
		}
		
		shuffle();
	}
	
	public Card[] getCards()
	{
		return cards;
	}
	

	public void shuffle()
	{
		Random rand = new Random();
		Card[] newDeck = new Card[cards.length];
		int number = 0;
		int arrayLength = cards.length;
		
		for (int ii=0 ; ii < cards.length ; ii++)
		{
			number = rand.nextInt(cards.length-ii);		
			newDeck[ii] = cards[number];	
			cards[number] = cards[arrayLength-ii-1];
		} 
		
		for (int ii=0 ; ii < cards.length ; ii++)
			cards[ii] = newDeck[ii];
	}
}
